package com.jiuyin.dianjing.ui.fragment.third;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.ui.activity.community.CollegesListActivity;
import com.jiuyin.dianjing.ui.activity.community.OnlineListActivity;
import com.jiuyin.dianjing.ui.activity.community.TagActivity;
import com.jiuyin.dianjing.ui.fragment.base.BaseFragment;
import com.jiuyin.dianjing.ui.fragment.community.PostFragment;
import com.jiuyin.dianjing.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_GENRE_NORMAL;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_TYPE_HOT;
import static com.jiuyin.dianjing.ui.activity.community.TagActivity.KEY_DESIRE_TAG;
import static com.jiuyin.dianjing.ui.activity.community.TagActivity.KEY_GAME_TAG;


public class FragmentThirdInner extends BaseFragment {
    public static final int REQUEST_TAG_CHOOSE = 0X458;
    @BindView(R.id.tv_hot_tag)
    TextView mTvHotTag;
    private String mType;
    private PostFragment mPostFragment;
    private String mGameTag = "";
    private String mDesireTag = "";

    public static FragmentThirdInner newInstance(String type) {
        FragmentThirdInner fragment = new FragmentThirdInner();
        Bundle args = new Bundle();
        args.putString(KEY_TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_third_inner;
    }

    @Override
    protected void initData() {
        if (getArguments() != null) {
            mType = getArguments().getString(KEY_TYPE, VALUE_TYPE_HOT);
        }
    }

    @Override
    protected void initView() {
        mPostFragment = PostFragment.newInstance(mType, VALUE_GENRE_NORMAL, "", mGameTag, mDesireTag);
        getChildFragmentManager().beginTransaction()
                .replace(R.id.layout_fragment, mPostFragment)
                .commit();
    }

    @Override
    public void fetchData() {

    }

    @OnClick({R.id.tv_online, R.id.tv_colleges, R.id.tv_bar, R.id.tv_whole_tag})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_online:
                goTargetActivity(OnlineListActivity.class);
                break;
            case R.id.tv_colleges:
                goTargetActivity(CollegesListActivity.class);
                break;
            case R.id.tv_bar:
                ToastUtil.showShort(getString(R.string.come_soon));
                break;
            case R.id.tv_whole_tag:
                Intent intent = new Intent(getContext(), TagActivity.class);
                intent.putExtra(KEY_GAME_TAG, mGameTag);
                intent.putExtra(KEY_DESIRE_TAG, mDesireTag);
                startActivityForResult(intent, REQUEST_TAG_CHOOSE);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_TAG_CHOOSE) {
                if (data == null) {
                    return;
                }
                String gameTag = data.getStringExtra(KEY_GAME_TAG);
                String desireTag = data.getStringExtra(KEY_DESIRE_TAG);
                if (mDesireTag.equals(desireTag) && mGameTag.equals(gameTag)) {
                    return;
                }
                mGameTag = gameTag;
                mDesireTag = desireTag;
                if (TextUtils.isEmpty(mGameTag) && TextUtils.isEmpty(mDesireTag)) {
                    mTvHotTag.setText(R.string.hot_labels);
                } else {
                    StringBuilder stringBuilder = new StringBuilder();
                    if (!TextUtils.isEmpty(desireTag)) {
                        stringBuilder.append(getString(R.string.tag, desireTag)).append(" ");
                    }
                    if (!TextUtils.isEmpty(gameTag)) {
                        stringBuilder.append(getString(R.string.tag, gameTag));
                    }
                    mTvHotTag.setText(stringBuilder.toString());
                }
                mPostFragment.setGenreTag(desireTag);
                mPostFragment.setGameTag(gameTag);
                mPostFragment.refresh();
            }
        }
    }
}
